home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / qrymod / integrity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-30  |  4.1 KB  |  197 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <catalog.h>
  4. # include    <access.h>
  5. # include    <tree.h>
  6. # include    <symbol.h>
  7. # include    "qrymod.h"
  8. # include    <sccs.h>
  9.  
  10. SCCSID(@(#)integrity.c    8.4    5/30/88)
  11.  
  12. /*
  13. **  INTEGRITY.C -- Integrity Constraint Processor
  14. **
  15. **    This module contains the integrity constraint processor.  This
  16. **    processor modifies the query to add integrity constraints.
  17. **
  18. **    Currently only single-variable aggregate-free constraints are
  19. **    handled.  Thus the algorithm is reduced to scanning the tree
  20. **    for each variable modified and appending the constraints for
  21. **    that variable to the tree.
  22. **
  23. **    Parameters:
  24. **        none
  25. **
  26. **    Returns:
  27. **        The root of the modified tree.
  28. **
  29. **    Side Effects:
  30. **        Much relation I/O.
  31. **        Modifies the tree in place, so the previous one is
  32. **            lost.
  33. **
  34. **    Trace Flags:
  35. **        40 -> 49
  36. */
  37.  
  38. extern DESC    Intdes;
  39.  
  40. QTREE *
  41. integrity(root)
  42. QTREE    *root;
  43. {
  44.     register QTREE        *r;
  45.     short            dset[8];
  46.     register QTREE        *p;
  47.     register int        i;
  48.     auto QTREE        *iqual;
  49.     struct integrity    inttup, intkey;
  50.     struct tup_id        hitid, lotid;
  51.     extern QTREE        *gettree();
  52.     extern QTREE        *norml();
  53.  
  54. #    ifdef xQTR1
  55.     tTfp(40, -1, "\n->INTEGRITY\n");
  56. #    endif
  57.  
  58.     r = root;
  59.  
  60.     /*
  61.     **  Check to see if we should apply the integrity
  62.     **  algorithm.
  63.     **
  64.     **  This means checking to insure that we have an update
  65.     **  and seeing if any integrity constraints apply.
  66.     */
  67.  
  68.     if (Qt.qt_qmode == mdRETR || Qt.qt_qmode == mdRET_UNI ||
  69.         (Qt.qt_rangev[Qt.qt_resvar].rngvdesc->reldum.relstat & S_INTEG) == 0)
  70.     {
  71. #        ifdef xQTR2
  72.         tTfp(40, 0, "->INTEGRITY: no integ\n");
  73. #        endif
  74.         return(r);
  75.     }
  76.  
  77.     /*
  78.     **  Create a set of the domains updated in this query.
  79.     */
  80.  
  81.     for (i = 0; i < 8; i++)
  82.         dset[i] = 0;
  83.     for (p = r->left; p != NULL && p->sym.type != TREE; p = p->left)
  84.     {
  85. #        ifdef xQTR3
  86.         if (p->sym.type != RESDOM)
  87.             syserr("integrity: RESDOM %d", p->sym.type);
  88. #        endif
  89.         lsetbit(p->sym.value.sym_resdom.resno, dset);
  90.     }
  91.  
  92. #    ifdef xQTR3
  93.     if (p == NULL)
  94.         syserr("integrity: NULL LHS");
  95. #    endif
  96. #    ifdef xQTR1
  97.     if (tTf(40, 1))
  98.         pr_set(dset, "dset");
  99. #    endif
  100.     
  101.     /*
  102.     **  Scan integrity catalog for possible tuples.  If found,
  103.     **  include them in the integrity qualification.
  104.     */
  105.  
  106.     iqual = NULL;
  107.     opencatalog("integrities", OR_READ);
  108.     setkey(&Intdes, &intkey, Qt.qt_rangev[Qt.qt_resvar].rngvdesc->reldum.relid, INTRELID);
  109.     setkey(&Intdes, &intkey, Qt.qt_rangev[Qt.qt_resvar].rngvdesc->reldum.relowner, INTRELOWNER);
  110.     find(&Intdes, EXACTKEY, &lotid, &hitid, &intkey);
  111.  
  112.     while ((i = get(&Intdes, &lotid, &hitid, &inttup, TRUE)) == 0)
  113.     {
  114.         if (kcompare(&Intdes, &intkey, &inttup) != 0)
  115.             continue;
  116.  
  117. #        ifdef xQTR1
  118.         if (tTf(40, 2)) {
  119.             printup(&Intdes, &inttup);
  120.             pr_set(inttup.intdomset, "tset");
  121.         }
  122. #        endif
  123.         
  124.         /* check for some domain set overlap */
  125.         for (i = 0; i < 8; i++)
  126.             if ((dset[i] & inttup.intdomset[i]) != 0)
  127.                 break;
  128.  
  129.         if (i >= 8)
  130.             continue;
  131.         
  132.         /* some domain matches, include in integrity qual */
  133.         i = Qt.qt_resvar;
  134.         p = gettree(Qt.qt_rangev[i].rngvdesc->reldum.relid,
  135.                 Qt.qt_rangev[i].rngvdesc->reldum.relowner,
  136.                 mdINTEG, inttup.inttree, FALSE);
  137. #        ifdef xQTR1
  138.         if (tTf(40, 3))
  139.             treeprn(p, "int_qual");
  140. #        endif
  141.  
  142.         /* trim off (null) target list */
  143.         p = p->right;
  144.  
  145.         /* merge the 'integrity' var into the Qt.qt_resvar */
  146.         i = inttup.intresvar;
  147.         if (Qt.qt_remap[i] >= 0)
  148.             i = Qt.qt_remap[i];
  149.         mergevar(i, Qt.qt_resvar, p);
  150.  
  151.         /* add to integrity qual */
  152.         if (iqual == NULL)
  153.             iqual = p;
  154.         else
  155.             appqual(p, iqual);
  156.     }
  157.     if (i < 0)
  158.         syserr("integrity: get %d", i);
  159.     
  160.     /*
  161.     **  Clean up the integrity qualification so that it will merge
  162.     **  nicely into the tree, and then append it to the user's
  163.     **  qualification.
  164.     */
  165.  
  166.     if (iqual != NULL)
  167.     {
  168.         /* replace VAR nodes by corresponding user afcn */
  169.         subsvars(&iqual, Qt.qt_resvar, r->left, Qt.qt_qmode);
  170.  
  171.         /* append to tree and normalize */
  172.         appqual(iqual, r);
  173. #        ifdef xQTR3
  174.         if (tTf(40, 8))
  175.             treeprn(r, "Unnormalized tree");
  176. #        endif
  177.         r->right = norml(trimqlend(r->right));
  178.     }
  179.  
  180. #    ifdef xQTR1
  181.     if (tTf(40, 15))
  182.         treeprn(r, "INTEGRITY->");
  183. #    endif
  184.  
  185.     return (r);
  186. }
  187.  
  188. # ifdef xQTR1
  189. treeprn(p, str)
  190. QTREE    *p;
  191. char    *str;
  192. {
  193.   printf("%s\n", str);
  194.   treepr(p);
  195. }
  196. # endif
  197.